#include "../util/logger.h"

static log4cpp::Category& logger = ::log4cpp::Category::getRoot();

namespace wilson::util
{
    void log_init(::std::string_view val)
    {
        ::log4cpp::Priority::PriorityLevel pl = 
            ::log4cpp::Priority::DEBUG;
        if (val == "emerg")
        {
            pl = ::log4cpp::Priority::EMERG;
        }
        else if (val == "fatal")
        {
            pl = ::log4cpp::Priority::FATAL;
        }
        else if (val == "alert")
        {
            pl = ::log4cpp::Priority::ALERT;
        }
        else if (val == "crit")
        {
            pl = ::log4cpp::Priority::CRIT;
        }
        else if (val == "error")
        {
            pl = ::log4cpp::Priority::ERROR;
        }
        else if (val == "warn")
        {
            pl = ::log4cpp::Priority::WARN;
        }
        else if (val == "notice")
        {
            pl = ::log4cpp::Priority::NOTICE;
        }
        else if (val == "info")
        {
            pl = ::log4cpp::Priority::INFO;
        }
        else if (val == "debug")
        {
            pl = ::log4cpp::Priority::DEBUG;
        }
        else if (val == "notset")
        {
            pl = ::log4cpp::Priority::NOTSET;
        }

        log_init(pl);
    }

    void log_init(::log4cpp::Priority::PriorityLevel p)
    {
        ::std::string pattern{ "%m%n" };
        auto* appender = new log4cpp::OstreamAppender{ "stdout", &::std::cout };
        auto* patter   = new log4cpp::PatternLayout{};
        patter->setConversionPattern(pattern);
        appender->setLayout(patter);
        logger.setPriority(p);
        logger.setAppender(appender);
    }
}
